<extend name="layout/edit" />

<block name="HEADER">
	<style type="text/css">
		.xw-form .layui-form{
			overflow: inherit;
		}
		.layui-form-selected .layui-edge{
			margin-top: 0 !important;
		}
		.xw-form .xw_searchs_bodys{
			line-height: 30px;padding: 10px;padding-bottom: 5px;position: relative;
		}
		.file-img {
			height: 100%;
			background-position: left;
			background-size: 100% 85%;
			background-repeat: no-repeat;
			cursor: pointer;
		}
		#selectGoodsGrid .layui-btn{ display: none; }
		.selectBtnRow{
			width: 40px;background: #f2f2f2;text-align: center;position: relative;
		}
		.selectBtnRow-boy{
			position: absolute;top: 50%;margin-top: -50px;width: 100%;
		}
		.selectBtnRow-boy div{
			padding: 5px 0;
		    background: #1e9fff;
		    margin-bottom: 10px;
		    color: #fff;
		}
		.selectBtnRow-boy div:hover{
			color: #0000FF;
			cursor: pointer;
		}
	</style>
</block>

<block name="CONTENT">
	<div id="form" padding="" height="" style="" class="xw-form">
		<div class="wrapper" style="height: calc(100% - 42px);">
			<div class="panel-heading xw_searchs_bodys">
				<control:search id="selectBillGoodsSearchs" />
			</div>
			<div class="content" style="height: calc(100% - 45px);padding: 5px;display: flex;">
				<div style="width: calc(100%);">
					<control:grid id="selectBillGoodsGrid" padding="0" height="100%" />
				</div>
				<!--<div class="selectBtnRow">
					<div class="selectBtnRow-boy" >
						<div onclick="page.add()">>></div>
						<div onclick="page.del()"><<</div>
					</div>
				</div>
				<div style="width: calc(50% - 20px);">
					<control:grid2 id="selectedGoodsGrid" padding="0" height="100%" />
				</div>-->
			</div>
		</div>
		<div class="xw-btnRow" style="text-align: right;">
			<button type="button" class="layui-btn layui-btn-normal layui-btn-sm " id="btnSave" onclick="page.save()" ><i class="iconfont" style="line-height: 0;"></i>确认选择</button> 
			<button type="button" class="layui-btn layui-btn-normal layui-btn-sm " id="close" onclick="$.close()"> <i class="iconfont" style="line-height: 0;"></i>关闭 </button>
		</div>
	</div>

</block>

<block name="FOOTER">
	<script type="text/javascript">
		var page = (function(ns) {
			
			ns.selectedIds = [];
			
			ns.init = function(){

				//动态给参数给selectBillGoodsGrid控件

                ns.query();
			}
			
			//搜索事件
		    ns.query=function(){
		    	var filter = selectBillGoodsSearchs.getData();
		    	filter.push({"field":"specs_id","opera":"not in","value": $.getRequest("specs_id") });
		    	if($.getRequest('type')){
		    		filter.push({"field":"type","opera":"=","value": $.getRequest('type') });
		    	}

		    	filter.push({"u_id":$.getRequest('uid'),'bill_type':$.getRequest('bill_type') });
                selectBillGoodsGrid.load( filter );
		    }
		    
			//添加
			ns.add = function(){
				var ids = $.map(selectBillGoodsGrid.getSelectedRows(),function(h){
					return h.id;
				});
				var ids2 = $.map(selectBillGoodsGrid.getRows(),function(h){
					return h.id;
				})
				
				if(!ids[0]){
					$.toast(0,"请选择需要添加的商品");
					return false;
				}
				ns.selectedIds = $.uniques(ns.selectedIds.concat(ids));
				ns.selectedIds = $.uniques(ns.selectedIds.concat(ids2));
				var oids = '';	
				if($.getRequest('type') != 2){
					oids = ns.selectedIds;
				}else{
					oids = $.uniques(ids.concat(ids2));
				}
                selectBillGoodsGrid.load([{"field":"id","opera":"in","value": oids.join(',') }]);
				ns.query();
			}
			
			//删除
			ns.del = function(){
				var ids = $.map(selectBillGoodsGrid.getSelectedRows(),function(h){
					return h.id;
				});
				if(!ids[0]){
					$.toast(0,"请选择需要移除的商品");
					return false;
				}
				
				ns.selectedIds = $.map(ns.selectedIds,function(id){
					if( ids.indexOf(id) == -1 && ids.indexOf( Number(id) ) == -1 ){
						return id;
					}
				});

                selectBillGoodsGrid.load([{"field":"id","opera":"in","value": ns.selectedIds.join(',') }]);
				ns.query();
			}
			
			//确定事件
			ns.save = function() {

                var h_paynum=false;
                var h_count=0;
                var p_num=0;
                var h_stocks=0;
                var h_stock=false;
                var res_data = $.map(selectBillGoodsGrid.getRows(),function(h){

                    if(h.pay_num>0){
                        if(h.pay_num>h.stock){
                            h_stock=true;
                        }
                        h_paynum=true;
                        h_count++;
                        p_num=h.pay_num;
                        h_stocks=h.stock;
                        return h;
                    }

                })


                if(h_stock){
                    $.toast(0,"购买数量不能大于库存数！");
                    return false;
                }

               if(!h_paynum){
                   $.toast(0,"输入购买数量！");
                   return false;
			   }


                //套餐类3：只能是单件，一对一个数量1。服务类2：是单一下单，可以多个数量。普通类1：可以多对多。
				if($.getRequest('bill_type')==2 && h_count>1){

                    $.toast(0,"服务类只能购买一样商品！");
                    return false;
				}
                if(($.getRequest('bill_type')==3 && p_num>1) || ($.getRequest('bill_type')==3 && h_count>1)){

                    $.toast(0,"套餐类只能购买一样商品，一件数量！");
                    return false;
                }

                // //判断选中的库存是否不为0
				if( h_stocks==0 ){
                    $.toast(0,"不能选择库存为空的商品！");
                    return false;
				}

				//异步调用结算接口,并且进行验证。
                $.ajaxs('/admin/v1/order/settlement',{
                    uid:$.getRequest('uid'),
                    data:res_data,
                    coupon_id:0
                }).then(function(res){
                    if( parent.page && parent.page.selectGoodsCallback){
                        if(parent.page.selectGoodsCallback(res_data) !== false){
                            $.close();
                        }
                    }else{
                        $.close();
                    }
                })



			}

			return ns;
		}({}));

		$(function() { page.init(); });
	</script>
</block>